package blog.gui;

import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

import blog.ConstString;
import blog.SysRuntime;
import blog.controller.ConstGui;
import blog.util.StringUtils;

public class HtmlInsertOnlineImageGui extends Form implements CommandListener,
		IBaseGui, ItemStateListener {

	private static Command cmdBack = new Command( ConstString.CMD_BACK, Command.BACK, 0 );
	private static Command cmdOk = new Command( ConstString.CMD_INSERT_INPUT_ONLY, Command.OK, 0 );
	
	TextField url;
	TextField description;
	ChoiceGroup specifySize;
	TextField width;
	TextField height;
	
	StringItem showResult;
	
	int insertPos;
	
	public HtmlInsertOnlineImageGui( int insertPos )
	{
		super( ConstString.GUI_TITLE_HTML_INSERT_LINK_GUI );
		url = new TextField( ConstString.GUI_HTML_INSERT_ONLINE_IMAGE_URL, "http://", 200, TextField.URL );
		description = new TextField( ConstString.GUI_HTML_INSERT_ONLINE_IMAGE_DESCRIPTION, "", 50, TextField.ANY );
		specifySize = new ChoiceGroup("", Choice.MULTIPLE );
		specifySize.append(ConstString.GUI_HTML_INSERT_ONLINE_IMAGE_SPECIFY_SIZE, null);
		width = new TextField( ConstString.GUI_HTML_INSERT_ONLINE_IMAGE_WIDTH, "100", 50, TextField.NUMERIC );
		height = new TextField( ConstString.GUI_HTML_INSERT_ONLINE_IMAGE_HEIGHT, "100", 50, TextField.NUMERIC );
		
		showResult = new StringItem(ConstString.GUI_HTML_INSERT_LINK_GUI_SHOW_RESULT_TITLE, calculateResult());
		
		append(url);
		append(description);
		append(specifySize);
		append(showResult);
		
		addCommand(cmdBack);
		addCommand(cmdOk);
		setCommandListener(this);
		
		setItemStateListener(this);
		
		this.insertPos = insertPos;
	}
	
	String calculateResult()
	{
		String param1 = url.getString();
		String param2 = description.getString();
		String param3 = (specifySize.isSelected(0))?StringUtils.combin(ConstString.GUI_HTML_INSERT_ONLINE_IMAGE_SIZE, width.getString(), height.getString()):"";
		
		return StringUtils.combin(ConstString.GUI_HTML_INSERT_ONLINE_IMAGE_SHOW_RESULT, new String[]{param1, param2, param3});
	}

	public int getIdentify() {
		return ConstGui.GUI_ID_HTML_INSERT_LINK;
	}

	public void notifyChanged(Object param) {

	}

	public void commandAction(Command cmd, Displayable arg1) {
		if ( cmd==cmdBack )
		{
			SysRuntime.guiManager.back();
		}
		else if ( cmd==cmdOk )
		{
			AdvanceBodyEdit.getInstance().insertSomething(showResult.getText(), insertPos);
			SysRuntime.guiManager.back();
		}
	}

	public void itemStateChanged(Item item) {
		if ( item==specifySize )
		{
			if ( specifySize.isSelected(0) )
			{
				insert(size()-1, width);
				insert(size()-1, height);
			}
			else
			{
				delete(size()-2);
				delete(size()-2);
			}
		}
		
		showResult.setText(calculateResult());
	}

}
